package cn.iocoder.yudao.framework.business.footprint.service;

import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.service.impl.AbstractService;
import cn.iocoder.yudao.framework.business.footprint.dao.FootprintMapper;
import cn.iocoder.yudao.framework.business.footprint.pojo.po.FootprintEntity;
import cn.iocoder.yudao.framework.business.footprint.pojo.vo.AppFootprintVO;
import cn.iocoder.yudao.framework.common.exception.KnownServiceException;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;

import java.io.Serializable;
import java.util.List;

/**
 * 足迹/浏览记录 Service 实现类，由子类继承，子类标注 @{@link org.springframework.stereotype.Service}
 * *
 * * @author 山野羡民
 */
//@org.springframework.stereotype.Service
public abstract class AppFootprintBaseService<SNAPSHOT extends Serializable, DO extends FootprintEntity, VO extends AppFootprintVO> extends AbstractService<DO, VO> implements AppFootprintService<SNAPSHOT, VO> {

    @Override
    public boolean save(Long bizId, Long userId) {
        int ret;
        SNAPSHOT snapshot = getSnapshot(bizId);
        CoreMapper<DO> mapper = getInjectedMapper();
        if (mapper instanceof FootprintMapper<DO> footprintBaseMapper) {
            DO entity = footprintBaseMapper.selectOneByBizIdAndUserId(bizId, userId, null);
            if (entity == null) {
                // 注意：需确保 DO 有无参构造函数，例如使用 @@NoArgsConstructor 注解
                entity = ReflectUtil.newInstance(resolveEntityClass());
                entity.setBizId(bizId);
                entity.setUserId(userId);
                entity.setSnapshot(JsonUtils.toJsonString(snapshot));
                entity.setRecycled(false);
                ret = mapper.insert(entity);
            } else {
                entity.setSnapshot(JsonUtils.toJsonString(snapshot));
                entity.setRecycled(false);
                ret = mapper.updateById(entity);
            }
            return ret > 0;
        }
        throw new KnownServiceException("Mapper 必须是" + FootprintMapper.class.getSimpleName() + " 的子类");
    }

    @Override
    public boolean delete(Long bizId, Long userId) {
        CoreMapper<DO> mapper = getInjectedMapper();
        if (mapper instanceof FootprintMapper<DO> footprintBaseMapper) {
            DO entity = footprintBaseMapper.selectOneByBizIdAndUserId(bizId, userId, false);
            if (entity != null) {
                entity.setRecycled(true);
                int ret = getInjectedMapper().updateById(entity);
                return ret > 0;
            }
            return false;
        }
        throw new KnownServiceException("Mapper 必须是" + FootprintMapper.class.getSimpleName() + " 的子类");
    }

    @Override
    public boolean clear(Long userId) {
        List<DO> list = getInjectedMapper().selectList("user_id", userId);
        if (list != null && !list.isEmpty()) {
            list.forEach(entity -> entity.setRecycled(true));
            return getInjectedMapper().updateBatch(list);
        }
        return false;
    }

    @Override
    public boolean isTrue(Long bizId, Long userId) {
        CoreMapper<DO> mapper = getInjectedMapper();
        if (mapper instanceof FootprintMapper<DO> footprintBaseMapper) {
            DO result = footprintBaseMapper.selectOneByBizIdAndUserId(bizId, userId, false);
            return result != null;
        }
        throw new KnownServiceException("Mapper 必须是" + FootprintMapper.class.getSimpleName() + " 的子类");
    }

}
